﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using APA_DAO;
using DAO;
using TAM_SPORT;

namespace AMS
{
    public partial class FrmReport : Form
    {
        public FrmReport()
        {
            InitializeComponent();
            loadUsr();
            txtFromDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtToDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        }

        private void loadUsr()
        {
            /*ReportDao reportDao = new ReportDao();
            DataTable dataUsr = reportDao.getAllUserNotAdmin();
            cbbNhanVien.DataSource = dataUsr;
            DataRow row = dataUsr.NewRow();
            row["ID"] = -1;
            row["FULL_NAME"] = "ALL";
            dataUsr.Rows.InsertAt(row, 0);
            cbbNhanVien.DisplayMember = "FULL_NAME";
            cbbNhanVien.ValueMember = "ID";
            cbbNhanVien.SelectedIndex = 0;*/

            UserDao dao = new UserDao();
            DataTable table = dao.getUsers(string.Empty);
            if (table != null)
            {
                DataRow row = table.NewRow();
                row["ID"] = 0;
                row["FULL_NAME"] = "Tất cả";
                table.Rows.InsertAt(row, 0);

                cbbNhanVien.DataSource = table;
                cbbNhanVien.DisplayMember = "FULL_NAME";
                cbbNhanVien.ValueMember = "ID";
                if (LoginDao.adminFlag)
                {
                    cbbNhanVien.SelectedIndex = 0;
                    cbbNhanVien.Enabled = true;
                }
                else
                {
                    cbbNhanVien.SelectedValue = LoginDao.userID;
                    cbbNhanVien.Enabled = false;
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            /*DateTime fromDate = dateTuNgay.Value;
            DateTime toDate = dateDenNgay.Value;
            int usrID = (int)((DataRowView)cbbNhanVien.SelectedItem)["ID"];
            ReportDao reportDao = new ReportDao();
            DataTable dataRpt = reportDao.getReportThuChiTheoNgay(fromDate, toDate, usrID);
            dataGridView1.DataSource = dataRpt;*/
            loadGrid();
            
        }

        private bool validate()
        {
            bool errFlg = false;
            int i = 0;
            StringBuilder strBuilder = new StringBuilder();
            DateTime day;
            if (!string.IsNullOrEmpty(txtFromDate.Text))
            {
                if (!DateTime.TryParseExact(txtFromDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out day))
                {
                    i++;
                    strBuilder.Append(i.ToString() + ". Ngày bắt đầu sai định dạng ngày/tháng/năm.");
                    errFlg = true;
                }
            }

            if (!string.IsNullOrEmpty(txtToDate.Text))
            {
                if (!DateTime.TryParseExact(txtToDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out day))
                {

                    i++;
                    if (errFlg)
                    {
                        strBuilder.Append("\n");
                    }
                    strBuilder.Append(i.ToString() + ". Ngày kết thúc sai định dạng ngày/tháng/năm.");
                    errFlg = true;
                }

            }

            if (!string.IsNullOrEmpty(strBuilder.ToString()))
            {
                Error er = new Error(strBuilder.ToString());
                er.ShowDialog();
                er.Dispose();
            }
            return !errFlg;
        }

        private void loadGrid()
        {
            dtgResult.Rows.Clear();
            if (validate())
            {
                ReportDao dao = new ReportDao();
                DataTable table = dao.getReportThuChiTheoNgay(
                    string.IsNullOrEmpty(txtFromDate.Text) ? default(DateTime?) : DateTime.ParseExact(txtFromDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None),
                    string.IsNullOrEmpty(txtToDate.Text) ? default(DateTime?) : DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None),
                    int.Parse(cbbNhanVien.SelectedValue.ToString()));
                int i = 0;
                float tongChi = 0; 
                float tongThu = 0;
                float total = 0;
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {                        
                        tongChi += float.Parse(row["CHI_TIEU"].ToString()) + float.Parse(row["TRA_TIEN_COC"].ToString());
                        tongThu += float.Parse(row["TIEN_PHONG"].ToString()) + 
                                        float.Parse(row["TIEN_DIEN"].ToString()) + 
                                        float.Parse(row["TIEN_NUOC"].ToString()) +
                                        float.Parse(row["PHU_THU"].ToString()) + 
                                        float.Parse(row["THU_TIEN_COC"].ToString());
                        total+= tongThu;
                        total-= tongChi;
                        i = dtgResult.Rows.Add();
                        dtgResult.Rows[i].Cells[0].Value = i + 1;
                        dtgResult.Rows[i].Cells[1].Value = row["FULL_NAME"].ToString();
                        dtgResult.Rows[i].Cells[2].Value = row["NGAY_THU"] == System.DBNull.Value ? "" : DateTime.Parse(row["NGAY_THU"].ToString()).ToString("dd/MM/yyyy");
                        dtgResult.Rows[i].Cells[3].Value = row["TIEN_PHONG"] == System.DBNull.Value ? "" : float.Parse(row["TIEN_PHONG"].ToString()).ToString("#,##0.##");
                        dtgResult.Rows[i].Cells[4].Value = row["TIEN_DIEN"] == System.DBNull.Value ? "" : float.Parse(row["TIEN_DIEN"].ToString()).ToString("#,##0.##");
                        dtgResult.Rows[i].Cells[5].Value = row["TIEN_NUOC"] == System.DBNull.Value ? "" : float.Parse(row["TIEN_NUOC"].ToString()).ToString("#,##0.##");
                        dtgResult.Rows[i].Cells[6].Value = row["PHU_THU"] == System.DBNull.Value ? "" : float.Parse(row["PHU_THU"].ToString()).ToString("#,##0.##");
                        dtgResult.Rows[i].Cells[7].Value = row["CHI_TIEU"] == System.DBNull.Value ? "" : float.Parse(row["CHI_TIEU"].ToString()).ToString("#,##0.##");
                        dtgResult.Rows[i].Cells[8].Value = row["THU_TIEN_COC"] == System.DBNull.Value ? "" : float.Parse(row["THU_TIEN_COC"].ToString()).ToString("#,##0.##");
                        dtgResult.Rows[i].Cells[9].Value = row["TRA_TIEN_COC"] == System.DBNull.Value ? "" : float.Parse(row["TRA_TIEN_COC"].ToString()).ToString("#,##0.##");
                        dtgResult.Rows[i].Cells[10].Value = row["TIEN_MAT"] == System.DBNull.Value ? "" : float.Parse(row["TIEN_MAT"].ToString()).ToString("#,##0.##");
                        if (i % 2 == 1)
                        {
                            dtgResult.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                        }
                    }
                }
                txtTongChi.Text = tongChi.ToString("#,##0.##");
                txtTongThu.Text = tongThu.ToString("#,##0.##");
                txtToTal.Text = total.ToString("#,##0.##");
            }
            
        }
        private void btnExportToExcel_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, "Doesnt support yet!");
        }

        private void dateTuNgay_ValueChanged(object sender, EventArgs e)
        {
            txtFromDate.Text = dateTuNgay.Value.ToString("dd/MM/yyyy");
        }

        private void dateDenNgay_ValueChanged(object sender, EventArgs e)
        {
            txtToDate.Text = dateDenNgay.Value.ToString("dd/MM/yyyy");
        }

    }
}
